<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class LibTree {
	var $root = NULL;
	var $key = 'id';
	var $parent = 'parent';
	var $items = array();
	
	//constructor
	function LibTree() {
		
	}
	
	//methods
	function addItem($item) {
		$this->items[] = $item;
	}
	
	function updateItem($item) {
		foreach($this->items as $k => $v) {
			if( $v[$this->key] == $item[$ky] ) {
				$this->items[$k] = $item;
			}
		}
	}
	
	function deleteItem($item) {
		if( !is_array($item) ) {
			$item = array($key => $item);
		}
		$this->removeItem($item);
	}
	
	function removeItem($item) {
		foreach($this->items as $k => $v) {
			if( $v[$this->key] == $item[$ky] ) {
				unset($this->items[$k]);
			}
		}
		$this->items = array_values($this->items);
		reset($this->items);
	}
	
	//
	function getChild($id) {
		foreach($this->items as $k => $v) {
			if( $v[$this->parent] == $id ) {
				return $v;
			}
		}
		
		return NULL;
	}
	
	function getChildren($id) {
		$d = array();
		foreach($this->items as $k => $v) {
			if( $v[$this->parent] == $id ) {
				$d[] = $v;
			}
		}
		
		return $d;
	}
	
	function getParent($id) {
		foreach($this->items as $k => $v) {
			if( $v[$this->parent] == $id ) {
				return $v;
			}
		}
	}
	
	function getParents($id) {
		$d = array();
		foreach($this->items as $k => $v) {
			if( $v[$this->parent] == $id ) {
				$d[] = $v;
			}
		}
		
		return $d;
	}
	
	function getPath($id) {
		
	}
}
//
?>